package net.atomarrow.bs.interceptor;

import java.lang.reflect.Method;

import net.atomarrow.aop.ControllerInterceptor;
import net.atomarrow.aop.ServiceInterceptor;
import net.atomarrow.action.ActionInvocation;
import net.atomarrow.ioc.ActionContext;
import net.atomarrow.render.Render;

public class MyServiceInterceptor implements ServiceInterceptor,ControllerInterceptor{

    @Override
    public void beforeAction(Object obj, Method method, Object[] args) {
//        System.out.println("before service method:"+method.getName());


    }

    @Override
    public void afterAction(Object obj, Method method, Object[] args,Object result, Throwable e) {
//        System.out.println("after service method:"+method.getName());
    	e=ActionContext.getContext().getServiceException();
    	if(e!=null){
    		System.out.println("捕捉到异常 service error");
    		StringBuilder sb = new StringBuilder();
			Throwable c=e.getCause();
			if(c==null){
				c=e;
			};
			sb.append(c);
			sb.append("<br>");
			if(c!=null){
				for(StackTraceElement ste:c.getStackTrace()){
					sb.append(ste.toString());
					sb.append("<br>");
				}
			}
			System.out.println(sb.toString());
    	}
    }

	public Render intercept(ActionInvocation actionInvocation) throws Exception {
		System.out.println("hebing controller");
		return actionInvocation.invoke();
	}


}
